﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using Forest.Core.Events;
using Forest.Cat.Domain.Entities.Resumes;

namespace Forest.Cat.Domain.Services.Jobseekers
{
    public class JobseekerService : IJobseekerService
    {

        private readonly IRepository<Jobseeker> _jobseekerRepository;
        private readonly IRepository<Resume> _resumeRepository;
        private readonly IEventPublisher _eventPublisher;

        public JobseekerService(IRepository<Jobseeker> jobseekerRepository,
            IRepository<Resume> resumeRepository,
            IEventPublisher eventPublisher)
        {
            this._resumeRepository = resumeRepository;
            this._jobseekerRepository = jobseekerRepository;
            this._eventPublisher = eventPublisher;
        }

        public virtual Jobseeker GetUserByEmail(string email)
        {

            if (string.IsNullOrWhiteSpace(email))
                return null;

            var query = from j in _jobseekerRepository.Table
                        orderby j.Email
                        where j.Email == email
                        select j;
            var jobseeker = query.FirstOrDefault();

            return jobseeker;
        }

        #region 根据简历中的邮件地址获取人员信息
        /// <summary>
        /// 根据简历中的邮件地址获取人员信息
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public virtual Jobseeker GetUserByResumeEmail(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
                return null;

            var query = from j in _jobseekerRepository.Table
                        join resume in _resumeRepository.Table
                        on j.Id equals resume.JobseekerId
                        where resume.Email == email
                        select j;
            var jobseeker = query.FirstOrDefault();
            return jobseeker;
        } 
        #endregion

        public virtual void UpdateUser(Jobseeker jobseeker)
        {
            if (jobseeker == null)
                throw new ArgumentNullException("Jobseeker");

            _jobseekerRepository.Update(jobseeker);

            //event notification
            _eventPublisher.EntityUpdated(jobseeker);
        }

        public virtual void InsertUser(Jobseeker jobseeker)
        {
            if (jobseeker == null)
                throw new ArgumentNullException("Jobseeker");

            _jobseekerRepository.Insert(jobseeker);

            //event notification
            _eventPublisher.EntityInserted(jobseeker);

        }

        public virtual Jobseeker InsertGuestUser()
        {
            var user = new Jobseeker()
            {
                Id = Guid.NewGuid(), //此处须采番
                Email = "",
                IDCard = "0",
                IsVerifiedEmail = false,
                IsVerifiedMobile = false,
                FullName = "",
                Mobile = "",
                Password = "",
                PasswordSalt = null,
                DateCreated = DateTime.Now,
                PasswordFormat = 0,
                LastSignedIn = DateTime.Now
            };

            _jobseekerRepository.Insert(user);

            return user;
        }
    }
}
